package com.blt.car.journey;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.blt.car.R;
import com.blt.car.base.BaseTitleBarActivity;
import com.blt.car.base.ViewHolder;
import com.blt.car.utils.StringUtils;
import com.blt.car.widget.dialog.RedPackageDialog;

import blt.app_core.api.base.CoreEvent;
import blt.app_core.api.base.CoreManager;
import blt.app_core.api.journey.JourneyApi;
import blt.app_core.api.journey.bean.AppealInfo;
import blt.app_core.api.journey.bean.ExtrasVo;
import blt.app_core.api.journey.bean.Travel;
import blt.app_core.api.journey.bean.TravelDetail;
import blt.app_core.api.journey.bean.TravelOrder;
import blt.app_core.api.other.OutApi;

/**
 * Created by xgp on 2017/6/22.
 */

public class JourneyDetailActivity extends BaseTitleBarActivity implements View.OnClickListener {
    public static final String TRAVEL_ID = "com.blt.car.journey.travel_id";
    private TextView happen_day;
    private TextView fastCost;
    private TextView exchangeNo;
    private TextView carNo;
    private TextView inSite;
    private TextView inTime;
    private TextView outSite;
    private TextView outTime;
    private TextView travelDistance;
    private TextView travelTime;

    private TextView tcSubmit;
    private Button redLoot;

    private int travelId;

//    public AppealInfo tbAppeal;

    private TravelOrder travel;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_journey_detail;
    }


    @Override
    protected void initData() {
        super.initData();
        int id = getIntent().getExtras().getInt(TRAVEL_ID);
        CoreManager.getCore(JourneyApi.class).getDetail(id);
    }

    @Override
    protected void initView(ViewHolder holder) {
        mTitleBar.setTitle("行程详情");
        holder.get(R.id.journey_detail_complaint);
        redLoot = holder.get(R.id.journey_detail_loot_package);
        tcSubmit = holder.get(R.id.journey_detail_is_submit);
        happen_day = holder.get(R.id.journey_detail_happen_day);
        fastCost = holder.get(R.id.journey_detail_fast_cost);
        exchangeNo = holder.get(R.id.journey_detail_exchange_no);
        carNo = holder.get(R.id.journey_detail_car_no);
        inSite = holder.get(R.id.journey_detail_in_site);
        inTime = holder.get(R.id.journey_detail_in_time);
        outSite = holder.get(R.id.journey_detail_out_site);
        outTime = holder.get(R.id.journey_detail_out_time);
        travelDistance = holder.get(R.id.journey_detail_travel_distance);
        travelTime = holder.get(R.id.journey_detail_travel_time);
        holder.setOnClickListener(this, R.id.journey_detail_complaint, R.id.journey_detail_loot_package);
    }

    @CoreEvent(coreClientClass = JourneyApi.class)
    public void onDetail(TravelDetail travelDetail) {
        travelId = travelDetail.tbTravelOrder.id;
        travel = travelDetail.tbTravelOrder;
        happen_day.setText(StringUtils.getYear(travelDetail.tbTravelOrder.createTime));
        fastCost.setText(travelDetail.tbTravelOrder.amount + "元");
        exchangeNo.setText(travelDetail.tbTravelOrder.orderNo);
        carNo.setText(travelDetail.tbTravelOrder.carNo);
        String mInSite = travelDetail.tbTravelOrder.start.split("-")[1];
        inSite.setText(mInSite);
        inTime.setText(StringUtils.getTime(travelDetail.tbTravelOrder.startTime));
        String mOutSite = travelDetail.tbTravelOrder.end.split("-")[1];
        outSite.setText(mOutSite);
        outTime.setText(StringUtils.getTime(travelDetail.tbTravelOrder.endTime));
        travelDistance.setText(travelDetail.tbTravelOrder.distance + "km");
        int hour = (int) (Float.valueOf(travelDetail.tbTravelOrder.time) / 60);
        int second = (int) (Float.valueOf(travelDetail.tbTravelOrder.time) - hour * 60);
        if (hour == 0) {
            travelTime.setText(second + "分钟");
        }
        if (second == 0) {
            travelTime.setText(hour + "小时");
        }
        travelTime.setText(hour + "小时" + second + "分钟");

        //已提交的话能否点击进去再次提交
        if (travelDetail.tbAppeal == null) {
//            this.tbAppeal = travelDetail.tbAppeal;
            tcSubmit.setText("未提交");
        }
        if (travelDetail.canSnatchRedpacket) {
            redLoot.setTextColor(getResources().getColor(R.color.text_color_blue));
            redLoot.setEnabled(true);
        }


    }

    private boolean isLoot;
    @CoreEvent(coreClientClass = OutApi.class)
    public void onLoot(boolean isLoot) {
        this.isLoot = isLoot;
    }

    @CoreEvent(coreClientClass = JourneyApi.class)
    public void onComplaintSuccess() {
        tcSubmit.setText("已提交");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.journey_detail_loot_package:
                RedPackageDialog commonDialog = new RedPackageDialog(this, travelId);
                if (isLoot) {
                    toastShort("您已经抢过红包了");
                } else {
                    commonDialog.show();
                }
                break;
            case R.id.journey_detail_complaint:
                ExtrasVo vo = new ExtrasVo();
                vo.travel = travel;
                vo.type = ExchangeComplaintActivity.SUBMIT;
                ExchangeComplaintActivity.start(this, vo);
                break;
        }
    }
//    OpenRedListener listener;
//    public interface OpenRedListener {
//        boolean isOpenRed();
//    }
}
